package com.lovo.project.servlet.product;


import com.lovo.project.enums.ProductEnum;
import com.lovo.project.enums.UserCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ProductDO;
import com.lovo.project.service.ProductService;
import com.lovo.project.service.impl.ProductServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.sql.Date;

/**
 * @title 商品添加url
 * @author: chenqiang
 * @date: 2020/11/4 14:03
 */
@WebServlet(name = "ProductAddServlet",value = ("/product/productAdd"))
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             request.setCharacterEncoding("utf-8");
             response.setCharacterEncoding("utf-8");
             response.setContentType("text/html; charset=UTF-8");
             ProductService productService =new ProductServiceImpl();



             String productName =request.getParameter("productName");
             String productClass =request.getParameter("productClass");
             String productMaterialQuality =request.getParameter("productMaterialQuality");
             String productCuttingSpecification =request.getParameter("productCuttingSpecification");
             String productExclusiveSpecification =request.getParameter("productExclusiveSpecification");
             String productPackageSpecification =request.getParameter("productPackageSpecification");
             String productFlavor =request.getParameter("productFlavor");
             String productPurpose =request.getParameter("productPurpose");
             String productAddress =request.getParameter("productAddress");
             Date productProductionDate =Date.valueOf(request.getParameter("productProductionDate"));
             String productQualityGuaranteePeriod =request.getParameter("productQualityGuaranteePeriod");
             String productSupplier =request.getParameter("productSupplier");
             String productTip =request.getParameter("productTip");
             Integer productMoney =Integer.parseInt(request.getParameter("productMoney")) ;
             String productImage =request.getParameter("productImage");
             Integer shopId =Integer.parseInt(request.getParameter("shopId"));


            PrintWriter out = response.getWriter();
            try {
                productService.addProduct(new ProductDO(productName,productClass,productMaterialQuality,productCuttingSpecification,productExclusiveSpecification,productPackageSpecification, productFlavor,productPurpose,productAddress,productProductionDate,productQualityGuaranteePeriod,productSupplier,productTip,productMoney,productImage,shopId));

                out.print(ResultVO.success().toJson());
            } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
            }catch (Exception e1){
            out.print(ResultVO.fail(ProductEnum.PRODUCT_ADD_ENUM).toJson());
            }finally {
            out.close();
            }

    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request,response);
    }


}
